﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlTypes;
using XMK.Framework.DataAccess;
using XMK.Tadong.Model;
using XMK.Tadong.Model.Cond;
using XMK.Utils;
using XMK.Framework.Model;



namespace XMK.Tadong.Data
{
    public class DemoData
    {
        /// <summary>
        /// 数据库连接
        /// </summary>
        protected DbProvider dbProvider;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbProvider"></param>
        public DemoData(DbProvider dbProvider)
        {
            this.dbProvider = dbProvider;
        }

        /// <summary>
        /// 添加金点子
        /// </summary>
        /// <param name="info">金点子对象</param>
        public JindianziAddInfo AddJinDianZi(string userId, JinDianZiCond cond)
        {
            JindianziAddInfo jindianziAddInfo = new JindianziAddInfo();
            //新建一个数据库操作帮助类
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, "gsp_post",
                //参数生成
                delegate()
                {
                    return DbCmdHelper.CreateParameters(dbProvider, "Subject", "UserID",
                        "UserName", "Message", "Fmodule", "Smodule", "PageTitle", "PageUrl", "FilePath", "FileName","Channel").ToArray();
                });

            //参数设值
            cmd.SetParamValues(cond.subject, userId, cond.userName,
                cond.message, cond.fmodule, cond.smodule, "", "", cond.filePath ?? "", cond.fileName ?? "", "junhong");
            //执行存储过程
            jindianziAddInfo = DbManager.GetInfo<JindianziAddInfo>(cmd, delegate(out JindianziAddInfo model, DataRow row)
            {
                model = new JindianziAddInfo();
                model.MessageInfo = ParseHelper.Parse<string>(row["Message"]);

            });
            return jindianziAddInfo;

        }

        public JinDianZiCountInfo CountJinDianZi(string userId)
        {
            JinDianZiCountInfo jindianzicount = new JinDianZiCountInfo();
            //新建一个数据库操作帮助类
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, "gsp_ScoreCount",
                //参数生成
                delegate()
                {
                    return DbCmdHelper.CreateParameters(dbProvider, "UserID").ToArray();
                });

            //参数设值
            cmd.SetParamValues(userId);
            //执行存储过程
            jindianzicount = DbManager.GetInfo<JinDianZiCountInfo>(cmd, delegate(out JinDianZiCountInfo model, DataRow row)
            {
                model = new JinDianZiCountInfo();
                model.JinDianziNum = ParseHelper.Parse<int>(row["JinDianziNum"]);
                model.AcceptJinDianziNum = ParseHelper.Parse<int>(row["AcceptJinDianziNum"]);
                model.ScoreCount = ParseHelper.Parse<int>(row["ScoreCount"]);
            });
            return jindianzicount;
        }
        /// <summary>
        /// 金点子列表
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="page"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public List<JingDianZiInfo> QueryJinDianZi(JingDianziQueryCond conn, PageInfo page, OrderInfo order)
        {
            List<JingDianZiInfo> jindianzi = new List<JingDianZiInfo>();
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, "gsp_PostQuery",
                delegate()
                {

                    return DbCmdHelper.CreateParameters(dbProvider, true, "Subject", "UserID", "UserName", "Smodule", "Fmodule", "Action", "StartTime", "EndTime").ToArray();

                });
            if (conn.EndTime != null && conn.StartTime != null)
            {
                cmd.SetParamValues(order, page, conn.Subject ?? "", conn.UserID ?? "", conn.UserName ?? "", conn.Smodule ?? "", conn.Fmodule ?? "", conn.Action ?? "", ParseHelper.ParseStrToDateTime(conn.StartTime), ParseHelper.ParseStrToDateTime(conn.EndTime));
            }
            else
            {
                if (conn.StartTime == null && conn.EndTime != null)
                {
                    cmd.SetParamValues(order, page, conn.Subject ?? "", conn.UserID ?? "", conn.UserName ?? "", conn.Smodule ?? "", conn.Fmodule ?? "", conn.Action ?? "", SqlDateTime.MinValue.Value, ParseHelper.ParseStrToDateTime(conn.EndTime));
                }
                else
                {
                    if (conn.StartTime != null && conn.EndTime == null)
                    {
                        cmd.SetParamValues(order, page, conn.Subject ?? "", conn.UserID ?? "", conn.UserName ?? "", conn.Smodule ?? "", conn.Fmodule ?? "", conn.Action ?? "", ParseHelper.ParseStrToDateTime(conn.StartTime), SqlDateTime.MaxValue.Value);
                    }
                    else
                    {
                        cmd.SetParamValues(order, page, conn.Subject ?? "", conn.UserID ?? "", conn.UserName ?? "", conn.Smodule ?? "", conn.Fmodule ?? "", conn.Action ?? "", SqlDateTime.MinValue.Value, SqlDateTime.MaxValue.Value);
                    }
                }
            }
            jindianzi = DbManager.GetPageInfos<JingDianZiInfo>(cmd, page, delegate(out JingDianZiInfo model, DataRow row)
            {
                model = new JingDianZiInfo();
                model.BlogID = ParseHelper.Parse<int>(row["BlogID"]);
                model.Subject = ParseHelper.Parse<string>(row["Subject"]);
                model.Fmodule = ParseHelper.Parse<string>(row["FmoduleName"]);
                model.ViewNum = ParseHelper.Parse<int>(row["ViewNum"]);
                model.ReplyNum = ParseHelper.Parse<int>(row["ReplyNum"]);
                model.UserID = ParseHelper.Parse<string>(row["UserID"]);
                model.UserName = ParseHelper.Parse<string>(row["UserName"]);
                model.Smodule = ParseHelper.Parse<string>(row["SmoduleName"]);
                model.IsJing = ParseHelper.Parse<string>(row["IsJing"]);
                model.IsUp = ParseHelper.Parse<string>(row["IsUp"]);
                model.CreateDateTime = ParseHelper.ParseDateTimeToStr((DateTime)row["CreateDateTime"]);
                model.Action = ParseHelper.Parse<string>(row["ActionName"]);
            });
            return jindianzi;
        }
        /// <summary>
        
    }
}
